export default {
    methods: {
        onopen({ msgId, inputText, refParams, ref }) {
            if (msgId) {
                this.contents[this.contents.length - 1].status = 'pending';
                this.contents[this.contents.length - 1].items = [];
                return;
            }
            if (this.contents[this.contents.length - 1].componentName === 'AimeInitContent') {
                this.contents.pop();
            }
            // 推送用户输入和AI开始返回的消息
            const placeContent = [{
                role: 'USER',
                msg_id: '',
                items: [
                    {
                        type: 'QUESTION',
                        body: {
                            query: inputText || this.inputText,
                            ref: refParams
                        }
                    }
                ],
                chartData: '',
                needTypeWriter: false,
                status: 'success',
                content: inputText || this.inputText,
                primarySelection: ref?.select_content || this.primarySelection,
                componentName: 'UserDialogBox'
            },
            {
                role: 'ASSISTANT',
                msg_id: '',
                items: [],
                chartData: '',
                needTypeWriter: true,
                status: 'pending',
                content: '',
                primarySelection: ref?.select_content || this.primarySelection,
                componentName: 'AimeDialogBox'
            }]
            this.contents.push(...placeContent);
            this.scrollToBottom();
            if (!inputText) {
                this.inputText = '';
            }
            // 清空选中项
            this.clearPrimarySelection();
        },
        onmessage(param) {
            try {
                const resData = JSON.parse(param.data);
                this.contents[this.contents.length - 1].items.push(resData.content);
                const { content, chart } = this.getStatusContent(
                    this.contents[this.contents.length - 1].items
                );
                this.contents[this.contents.length - 1].content = content;
                this.contents[this.contents.length - 1].chart = chart;
                this.contents[this.contents.length - 1].msg_id = resData.msg_id;
            } catch (error) {
                errorReport({
                    name: `onmessage中错误${error.name}`,
                    message: error.message || '',
                    stack: error.stack || ''
                });
            }
        },
        onclose() {
            const { status, content, chart } = this.getStatusContent(
                this.contents[this.contents.length - 1].items
            );
            this.contents[this.contents.length - 1].status = status;
            this.contents[this.contents.length - 1].content = content;
            this.contents[this.contents.length - 1].chart = chart;
        },
        onerror(apiParams, error) {
            this.contents[this.contents.length - 1].status = 'error';
            errorReport({
                name: `sendMessage函数的onerror中错误${error?.name}`,
                message: `请求入参为${apiParams}；${error?.message || ''}`,
                stack: error?.stack || ''
            });
        },
    },
}